﻿using DevExpress.XtraEditors;
using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.RootInterface.Field;
using EkStudio.com.bean.xml.Unit;
using EkStudio.com.ui.form;
using EkStudio.com.ui.form.AutoDataSource;
using EkStudio.com.ui.unitbill.property.Event;
using EkStudio.Com.UI.Interface;
using System.Text;
using System.Windows.Forms;

namespace EkStudio.com.ui.unitbill.property
{



    public partial class ModuleUiEventProperty : XtraUserControl
    {

        private ProjectRootPanel RootPanel = null;

        private ProjectUnitBillInterface UnitPanel = null;

        private BillFormatModule bean = null;

        private BillUiEventToProperty SelectEvent = null;

        private ModuleFieldSelectForm FieldForm = null;//模块选择框

        public event KEventHandler CaptionChanged = null;


        public ModuleUiEventProperty(BillFormatModule _bean, ProjectRootPanel root, ProjectUnitBillInterface unit)
        {
            InitializeComponent();
            this.bean = _bean;
            this.RootPanel = root;
            this.UnitPanel = unit;
            InitProperty();
        }

        private void InitProperty()
        {
            //初始化下拉框内容
            RepositoryFillType.Items.AddRange(BeanUtil.EVENTTYPE);

            //初始化属性编辑器的只读属性
            Event_01_Caption.Properties.ReadOnly = UnitPanel.ReadOnly;
            Event_02_Key.Properties.ReadOnly = UnitPanel.ReadOnly;
            Event_03_FillType.Properties.ReadOnly = UnitPanel.ReadOnly;
            Event_04_EventType.Properties.ReadOnly = UnitPanel.ReadOnly;
            Event_05_TargetTable.Properties.ReadOnly = UnitPanel.ReadOnly;
            Event_06_BindEvent.Properties.ReadOnly = UnitPanel.ReadOnly;
            Event_07_CoverWrite.Properties.ReadOnly = UnitPanel.ReadOnly;
            Event_08_EventSource.Properties.ReadOnly = UnitPanel.ReadOnly;
        }
        /// <summary>
        /// 刷新子表字段组列表
        /// </summary>
        private void InitGroupList()
        {
            RepositoryTableList.Items.Clear();
            if (bean.UiField != null && bean.UiField.FieldsProperties != null && bean.UiField.FieldsProperties.Count > 0)
            {
                for(int i = 0; i < bean.UiField.FieldsProperties.Count; i++)
                {
                    BillUiGroupItems group = bean.UiField.FieldsProperties[i];
                    if(group.Table)RepositoryTableList.Items.Add(group.Key);
                }
            }
        }


        public BillUiEventToProperty SelectObject
        {
            set
            {
                SelectEvent = value;
                PropertyGrid.SelectedObject = SelectEvent;
                //修改字段组列表
                InitGroupList();
            }
        }

        public BillUiEventToProperty[] SelectObjects
        {
            set
            {
                SelectEvent = new BillUiEventToProperty(value);
                PropertyGrid.SelectedObject = SelectEvent;
                //修改字段组列表
                InitGroupList();
            }
        }


        private void PropertyGrid_CellValueChanged(object sender, DevExpress.XtraVerticalGrid.Events.CellValueChangedEventArgs e)
        {
            UnitPanel.SaveStatus = false;
            if(e.Row.Properties.FieldName == "Caption")
            {
                if (this.CaptionChanged != null)
                {
                    PropertyDataEvent data = new PropertyDataEvent("", e.Value.ToString());
                    this.CaptionChanged(this, data);
                }
            }
        }
        /// <summary>
        /// 字段选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryFieldSelect_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if(FieldForm==null) { FieldForm = new ModuleFieldSelectForm(); }
            FieldForm.SetFormatBill(bean);
            DialogResult DR = FieldForm.ShowDialog();
            if(DR == DialogResult.OK)
            {
                StringBuilder text = new StringBuilder();
                BillUiFieldsItem[] fields = FieldForm.SelectFields;
                if (fields != null && fields.Length > 0)
                {
                    for(int i = 0; i < fields.Length; i++)
                    {
                        if (text.Length > 0) text.Append(",");
                        text.Append(fields[i].Key);
                    }
                }
                if (text.Length > 0)
                {
                    if (SelectEvent.BindEvent.Trim().Length > 0) { text.Insert(0, ","); text.Insert(0, SelectEvent.BindEvent); }
                    SelectEvent.BindEvent = text.ToString();
                    PropertyGrid.Refresh();
                    UnitPanel.SaveStatus = false;
                }
            }
        }
        /// <summary>
        /// 数据源设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RepositoryDataSource_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            AutoDataSourceForm AutoDataForm = new AutoDataSourceForm(bean,RootPanel,UnitPanel);
            AutoDataForm.SetSelectDataFill(SelectEvent.DataFill);
            DialogResult DR = AutoDataForm.ShowDialog(this.FieldForm);
            if(DR== DialogResult.OK)
            {
                SelectEvent.DataFill.SqlSource = AutoDataForm.FillSource;
                SelectEvent.DataFill.SqlWhere = AutoDataForm.FillWhere;
                SelectEvent.DataFill.SqlValue = AutoDataForm.FillValue;
                UnitPanel.SaveStatus = false;
                PropertyGrid.Refresh();
            }
            
        }
    }
}
